﻿/*!
* jQuery MessageBox Plugin
* Jquery.MessageBox.js
* Author : Gagan Deep sharma
* Date : 28-Aug-2012
* Version 1.1 
* Release Date : 30-Aug-2012
* @requires jQuery v1.2.3 or later, BlockUI Plugin
* Copyright (c) Gagan Deep Sharma
*/

///<reference path="jquery.blockUI.js" />
/// <reference path="Jquery.MessageBox.css" />

; (function ($) {

    if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.1/.test($.fn.jquery)) {
	    alert('blockUI requires jQuery v1.2.3 or later!  You are using v' + $.fn.jquery);
	    return;
    }

    $.FileType={    
        JS:"js",
        CSS:"css"
    };

    $.MessageBox = function(){}
    
    $.MessageBox.IncludeFile = function(jsFile,ftype) {
        if(ftype == $.FileType.JS)
        {
            $('head').append($('<script>').attr('type', 'text/javascript').attr('src', jsFile));
        }
        else if(ftype == $.FileType.CSS)
        {            
            $('head').append($('<link>').attr('type', 'text/css').attr('href', jsFile).attr('rel','stylesheet'));
        }
    }   
     
    $.MessageBox.IncludeFile("jquery.blockUI.js",$.FileType.JS);

    $.MessageBox.IncludeFile("Jquery.MessageBox.css",$.FileType.CSS);

    $.MessageBox.Version= "1.0";  

    $.MessageBox.Mode = {
        success:"success",
        error:"error",
        confirm:"confirm",
        warning:"warning"
    };

    $.MessageBox.defaults = {  
        obj:window,      
        message:  'Please pass any message to the plugin!!',	    
        title: "Information",	
	    mode: $.MessageBox.Mode.success
    };

    $.MessageBox.HTMLBody = {
    
    success :  "<div class='ContainerBox_Success'>"+
                    "<div class='TitleBox_Success'>"+
                        "<h3>"+
                            "##Title##</h3>"+
                    "</div><hr />"+
    
                    "<div class='BoxBody'>"+
                        "<h4>"+
                            "##Body##"+
                            "</h4>"+
                    "</div>"+
    
                    "<div class='BoxFooter'>"+
                        "<button style='width: 70px;' onclick='$.MessageBox.CloseBox();'>"+
                    "OK</button></div>"+
                "</div>",

    error: "<div class='ContainerBox_Error'>" +
                    "<div class='TitleBox_Error'>" +
                        "<h3>" +
                            "##Title##</h3>" +
                    "</div><hr />" +

                    "<div class='BoxBody'>" +
                        "<h4>" +
                            "##Body##" +
                            "</h4>" +
                    "</div>" +

                    "<div class='BoxFooter'>" +
                        "<button style='width: 70px;' onclick='$.MessageBox.CloseBox();'>" +
                    "OK</button></div>" +
                "</div>",

    warning: "<div class='ContainerBox_Warning'>" +
                    "<div class='TitleBox_Warning'>" +
                        "<h3>" +
                            "##Title##</h3>" +
                    "</div><hr />" +

                    "<div class='BoxBody'>" +
                        "<h4>" +
                            "##Body##" +
                            "</h4>" +
                    "</div>" +

                    "<div class='BoxFooter'>" +
                        "<button style='width: 70px;' onclick='$.MessageBox.CloseBox();'>" +
                    "OK</button></div>" +
                "</div>",

    confirm:   "<div class='ContainerBox_Confirm'>"+
                    "<div class='TitleBox_Confirm'>"+
                        "<h3>"+
                            "Confirmation</h3>"+
                    "</div><hr />"+
    
                    "<div class='BoxBody'>"+
                        "<h4>"+
                            "##Body##"+
                            "</h4>"+
                    "</div>"+
    
                    "<div class='BoxFooter'>"+
                        "<button style='width: 70px;' ##ClickEventCode## >"+
                    "OK</button>"+
                    "<button style='width: 70px;' onclick='$.MessageBox.CloseBox();return false;'>"+
                    "Cancel</button>"+                    
                    "</div>"+
                "</div>"  
                 
    };

    $.MessageBox.Show = function (obj,message,title,mode) {

       if(obj == undefined || obj == null ) obj = $.MessageBox.defaults.obj;
       if(message == undefined || message == null || $.trim(message) == '' || !message) message = $.MessageBox.defaults.message;
       if(title == undefined || title == null || $.trim(title) == '' || !title) title = $.MessageBox.defaults.title;
       if(mode == undefined || mode == null || $.trim(mode) == '' || !mode) mode = $.MessageBox.defaults.mode;

       var htmlBody =  "";

       if(mode == $.MessageBox.Mode.success)
       {
            htmlBody =  $.MessageBox.HTMLBody.success;
       }
       else if(mode == $.MessageBox.Mode.error)
       {
            htmlBody = $.MessageBox.HTMLBody.error;
       }
       else if(mode == $.MessageBox.Mode.confirm)
       {
            htmlBody = $.MessageBox.HTMLBody.confirm;
       }
       else if(mode == $.MessageBox.Mode.warning)
       {
            htmlBody = $.MessageBox.HTMLBody.warning;
       }

       htmlBody = htmlBody.replace("##Title##" , title);
       htmlBody = htmlBody.replace("##Body##" , message);

       if(mode == $.MessageBox.Mode.confirm){
        if(obj != null && obj != undefined && obj != '' && obj.id != null && obj.id != undefined && obj.id != ''){            
          var execscript = "onclick=\'$(\"#" + obj.id + "\").click();\'";
          htmlBody = htmlBody.replace("##ClickEventCode##" , execscript);
        }
       }

       $.blockUI({
            message: htmlBody,
            centerX: true,
            centerY: true,
            css: {
                //top: ($(window).height() - 400) / 2 + 'px',
                top: '30%',
                left: ($(window).width() - 400) / 2 + 'px'
            },
            allowBodyStretch: true           
       });
       
       if(mode == $.MessageBox.Mode.confirm)        
        return false;
       else 
        return true;
    }

    $.MessageBox.CloseBox = function(){    
       $.unblockUI();
    } 

})(jQuery);